package com.yamaikeji.system.util;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;

import java.util.Map;
import java.util.UUID;

public class PushUtil {

    private static String appId = "7HywUk6MYb7DsEy3QRzo86";
    private static String appKey = "RFoGtKqwLT6ZMiL7iJXtK9";
    private static String masterSecret = "tvIXExymYm7k5P4SPiKnC3";

    public static void bindAlias(String cid, String alias) {
        try {
            GtApiConfiguration apiConfiguration = new GtApiConfiguration();
            //填写应用配置
            apiConfiguration.setAppId(appId);
            apiConfiguration.setAppKey(appKey);
            apiConfiguration.setMasterSecret(masterSecret);
            // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
            apiConfiguration.setDomain("https://restapi.getui.com/v2/");
            // 实例化ApiHelper对象，用于创建接口对象
            ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
            // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
            UserApi userApi = apiHelper.creatApi(UserApi.class);
            CidAliasListDTO cidAliasListDTO = new CidAliasListDTO();
            CidAliasListDTO.CidAlias cidAlias = new CidAliasListDTO.CidAlias();
            cidAlias.setCid(cid);
            cidAlias.setAlias(alias);
            cidAliasListDTO.add(cidAlias);
            userApi.bindAlias(cidAliasListDTO);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            //logger.error(e.getMessage());
        }
    }

    public static void pushMsg(String alias, Alert alert) {
        try {
            GtApiConfiguration apiConfiguration = new GtApiConfiguration();
            //填写应用配置
            apiConfiguration.setAppId(appId);
            apiConfiguration.setAppKey(appKey);
            apiConfiguration.setMasterSecret(masterSecret);
            // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
            apiConfiguration.setDomain("https://restapi.getui.com/v2/");
            // 实例化ApiHelper对象，用于创建接口对象
            ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
            // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
            PushApi pushApi = apiHelper.creatApi(PushApi.class);

            //根据cid进行单推
            PushDTO<Audience> pushDTO = new PushDTO<Audience>();
            // 设置推送参数
            pushDTO.setRequestId(System.currentTimeMillis() + "");
            Settings settings = new Settings();
            Strategy strategy = new Strategy();
            strategy.setDef(1);
            //vip设置有效
            strategy.setIos(2);
            settings.setStrategy(strategy);
            pushDTO.setSettings(settings);

            /**** 设置厂商相关参数 ****/
            PushChannel pushChannel = new PushChannel();
            pushDTO.setPushChannel(pushChannel);

            /**** 设置个推通道参数 *****/
            PushMessage pushMessage = new PushMessage();
            //vip设置有效
            pushDTO.setPushMessage(pushMessage);
            GTNotification notification = new GTNotification();
            pushMessage.setNotification(notification);
            notification.setTitle(alert.getTitle());
            notification.setBody(alert.getBody());
            notification.setClickType("url");
            notification.setUrl("https://www.getui.com");
            /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/

            /*设置ios厂商参数*/
            IosDTO iosDTO = new IosDTO();
            pushChannel.setIos(iosDTO);
            // 相同的collapseId会覆盖之前的消息
            iosDTO.setApnsCollapseId(UUID.randomUUID().toString());
            Aps aps = new Aps();
            iosDTO.setAps(aps);
            aps.setSound("tip.mp3");
            aps.setAlert(alert);
            //alert.setTitle("ios title");
            //alert.setBody("ios body");
            /*设置ios厂商参数结束，更多参数请查看文档或对象源码*/

            /*设置接收人信息*/
            Audience audience = new Audience();
            pushDTO.setAudience(audience);
            //"5313a86cb2f84dae1d765a9fd4ee08e9"
            //audience.addCid(cid);
            audience.addAlias(alias);
            /*设置接收人信息结束*/
            /**** 设置厂商相关参数，更多参数请查看文档或对象源码 ****/

            // 进行alias单推
            ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByAlias(pushDTO);
            if (apiResult.isSuccess()) {
                // success
                System.out.println(apiResult.getData());
            } else {
                // failed
                System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            //logger.error(e.getMessage());
        }
    }
}
